package com.rchat.pocmini.activity.bluetoothwalkie.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.rchat.pocmini.R;
import com.rchat.pocmini.activity.bean.GroupBean;
import com.rchat.pocmini.activity.bluetoothwalkie.bean.WalkieChannelBean;

import java.util.List;

/**
 * @Description ：
 * @Author itsdf07
 * @Time 2017/10/12
 */

public class WalkieChannelAdapter extends RecyclerView.Adapter<WalkieChannelAdapter.WalkieChannelHolder> {
    private List<WalkieChannelBean> mChannelData;
    private ItemTouchHelperAdapterCallback mItemTouchHelperAdapterCallback;


    public interface ItemTouchHelperAdapterCallback {
        /**
         * Item点击事件
         *
         * @param view
         * @param bean
         */
        void onItemClick(View view, GroupBean bean);
    }

    public WalkieChannelAdapter(List<WalkieChannelBean> data, ItemTouchHelperAdapterCallback callback) {
        this.mChannelData = data;
        this.mItemTouchHelperAdapterCallback = callback;
    }

    public void setData(List<WalkieChannelBean> data) {
        this.mChannelData = data;
        notifyDataSetChanged();
    }

    @Override
    public WalkieChannelAdapter.WalkieChannelHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_channel, parent, false);
        return new WalkieChannelHolder(view);
    }

    @Override
    public void onBindViewHolder(final WalkieChannelAdapter.WalkieChannelHolder holder, int position) {
        final WalkieChannelBean bean = mChannelData.get(position);
        holder.channel.setText(bean.getChannelName());
        holder.channel.setSelected(bean.isSelecting());
        holder.channel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bean.setSelecting(!bean.isSelecting());
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mChannelData == null ? 0 : mChannelData.size();
    }

    class WalkieChannelHolder extends RecyclerView.ViewHolder {
        /**
         * 频道名称
         */
        private TextView channel;

        public WalkieChannelHolder(View itemView) {
            super(itemView);
            channel = (TextView) itemView.findViewById(R.id.layout_walkiechannel);
        }
    }
}
